package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Appointment;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IBusinessService businessService;


    @RequiredPermission(name="业务预约页面",expression="appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo){
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("business", businessService.listAll());
        model.addAttribute("appointmentStatusEnum", AppointmentStatusEnum.values());
        model.addAttribute("parents", systemDictionaryItemService.selectParentList("business"));
        return "appointment/list";
    }


    @RequiredPermission(name="业务预约删除",expression="appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    @RequiredPermission(name="业务预约新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {
        if (appointment.getId() == null) {
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }

    @RequiredPermission(name="用户端业务预约新增/编辑",expression="appointment:save")
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Appointment appointment) {
        appointmentService.userSave(appointment);
        return new JsonResult();
    }

    @RequiredPermission(name="业务预约新增/编辑跳转",expression="appointment:input")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("appointment",appointmentService.get(id));
        }
        return "appointment/input";
    }

    @RequiredPermission(name="业务预约修改状态",expression="appointment:checkStatus")
    @RequestMapping("/checkStatus")
    @ResponseBody
    public JsonResult checkStatus(Long id, Integer status) {
       appointmentService.checkStatus(id, status);
       return new JsonResult();
    }
}
